﻿/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

(function()
{
	// Map 'true' and 'false' values to match W3C's specifications
	var checkboxValues =
	{
		//fit : { 'true' : 'true', 'false' : 'false' },
		border : { 'true' : '1', 'false' : '0' }
		//plain:{ 'true' : 'true', 'false' : 'false' }
	};	
	var names = ['fit','plain'];
	for ( var i = 0 ; i < names.length ; i++ )
		checkboxValues[ names[i] ] = {'true' : 'true', 'false' : 'false'};

	function loadValue( spcNode )
	{
		var isCheckbox = this instanceof CKEDITOR.ui.dialog.checkbox;
		if ( spcNode.hasAttribute( this.id ) )
		{
			var value = spcNode.getAttribute( this.id );
			if ( isCheckbox )
				this.setValue( checkboxValues[ this.id ][ 'true' ] == value.toLowerCase() );
			else
				this.setValue( value );
		}
	}

	function commitValue( spcNode )
	{
		var isRemove = this.getValue() === '',
			isCheckbox = this instanceof CKEDITOR.ui.dialog.checkbox,
			value = this.getValue();
		if ( isRemove )
			spcNode.removeAttribute( this.att || this.id );
		else if ( isCheckbox )
			spcNode.setAttribute( this.id, checkboxValues[ this.id ][ value ] );
		else
			spcNode.setAttribute( this.att || this.id, value );
	}

	CKEDITOR.dialog.add( 'stab', function( editor )
	{
		var spcLang = editor.lang.spc,
			commonLang = editor.lang.common,
			dialogadvtab = editor.plugins.dialogadvtab;
		return {
			title : spcLang.stab.title,
			minWidth : 500,
			minHeight : 350,
			onShow : function()
			{
				// Clear previously saved elements.
				this.fakeImage = this.spcNode = null;

				var fakeImage = this.getSelectedElement();
				if ( fakeImage && fakeImage.data( 'cke-real-element-type' ) && fakeImage.data( 'cke-real-element-type' ) == 'stab' )
				{
					this.fakeImage = fakeImage;

					var spcNode = editor.restoreRealElement( fakeImage );
					this.spcNode = spcNode;

					this.setupContent( spcNode );
				}
			},
			onOk : function()
			{
				var spcNode;
				if ( !this.fakeImage ){				
					spcNode = new CKEDITOR.dom.element( 'img' );
					spcNode.setAttributes( {type:"spc_stab"});
				}
				else
					spcNode = this.spcNode;

				// A subset of the specified attributes/styles
				// should also be applied on the fake element to
				// have better visual effect. (#5240)
				var extraStyles = {}, extraAttributes = {};
				this.commitContent( spcNode, extraStyles, extraAttributes );

				// Refresh the fake image.
				var newFakeImage = editor.createFakeElement( spcNode, 'cke_stab', 'stab', true );
				newFakeImage.setAttributes( extraAttributes );
				newFakeImage.setStyles( extraStyles );
				if ( this.fakeImage )
				{
					newFakeImage.replace( this.fakeImage );
					editor.getSelection().selectElement( newFakeImage );
				}
				else
					editor.insertElement( newFakeImage );
			},
			contents : [
				{
					id : 'info',
					label : commonLang.generalTab,
					accessKey : 'I',
					elements :
					[
						{
							type : 'vbox',
							children :
							[
								{
									id : 'title',
									type : 'text',
									width:"80%",
									label : 'title',
									setup : loadValue,
									commit : commitValue
								}
							]
						},{
							type : 'hbox',
							children :	
							[
								{
									id : 'width',
									type : 'text',
									label : 'width',
									style : 'width:95px',
									validate : CKEDITOR.dialog.validate.htmlLength( editor.lang.common.invalidHtmlLength.replace( '%1', editor.lang.common.width ) ),
									setup : loadValue,									
									commit : commitValue
								},{
									id : 'height',
									type : 'text',
									label : 'height',
									style : 'width:95px',
									validate : CKEDITOR.dialog.validate.htmlLength( editor.lang.common.invalidHtmlLength.replace( '%1', editor.lang.common.height ) ),
									setup : loadValue,
									commit : commitValue
								},{
									id : 'scrollIncrement',
									type : 'text',
									label : 'scrollIncrement',
									style : 'width:95px',
									validate : CKEDITOR.dialog.validate.integer( "must integer" ),
									setup : loadValue,
									commit : commitValue
								},{
									id : 'scrollDuration',
									type : 'text',
									label : 'scrollDuration',
									style : 'width:95px',
									validate : CKEDITOR.dialog.validate.integer( "must integer"),
									setup : loadValue,
									commit : commitValue
								}
							]							
						},{
							type : 'hbox',
							children :	
							[
								{
									id : 'plain',
									type : 'checkbox',
									label : 'plain',
									setup : loadValue,
									commit : commitValue
								},{
									id : 'fit',
									type : 'checkbox',
									label : 'fit',
									setup : loadValue,
									commit : commitValue
								},{
									id : 'border',
									type : 'checkbox',
									label : 'border',
									setup : loadValue,
									commit : commitValue
								}
							]						
							
						},{
							type : 'vbox',
							children :
							[
								{
									id : 'tools',
									type : 'textarea',
									label : 'tools',
									setup : loadValue,
									commit : commitValue
								}
							]
						}
					]
				},{	id : 'events',
					label : spcLang.events,
					elements :
					[
						{
							type : 'hbox',
							children :
							[
								{
									id : 'onLoad',
									type : 'textarea',
									rows : 4,
									label : 'onLoad',
									setup : loadValue,
									commit : commitValue
								},{
									id : 'onSelect',
									type : 'textarea',
									rows : 4,
									label : 'onSelect',
									setup : loadValue,
									commit : commitValue
								}
							]
						},{
							type : 'hbox',
							children :
							[
								{
									id : 'onBeforeClose',
									type : 'textarea',
									rows : 4,
									label : 'onBeforeClose',
									setup : loadValue,
									commit : commitValue
								},{
									id : 'onClose',
									type : 'textarea',
									rows : 4,
									label : 'onClose',
									setup : loadValue,
									commit : commitValue
								}	
							]						
						},{
							type : 'hbox',
							children :
							[
								{
									id : 'onAdd',
									type : 'textarea',
									rows : 4,
									label : 'onAdd',
									setup : loadValue,
									commit : commitValue
								},{
									id : 'onUpdate',
									type : 'textarea',
									rows : 4,
									label : 'onUpdate',
									setup : loadValue,
									commit : commitValue
								}						
							]
						},{
							type : 'hbox',
							children :
							[
								{
									id : 'onContextMenu',
									type : 'textarea',
									rows : 4,
									label : 'onContextMenu',
									setup : loadValue,
									commit : commitValue
								}						
							]						
						}
					]				
				},
				dialogadvtab && dialogadvtab.createAdvancedTab( editor )
			]
		};
	});
})();
